/**
 * ********************************************************
 * <p>
 * Copyright (c) 2019 Baidu.com, Inc. All Rights Reserved
 * <p>
 * ********************************************************
 *
 * @date: 2019年8月8日
 * @author: cuixuefei
 * @description:
 */
package com.baidu.mpks.backendweb.ktree.web;

import com.baidu.mpks.backendweb.ktree.dao.TreeNodeDao;
import com.baidu.mpks.backendweb.ktree.domain.TreeNode;
import com.baidu.mpks.backendweb.ktree.dto.KTreeResponse;
import com.baidu.mpks.backendweb.ktree.dto.TreeNodeDto;
import com.baidu.mpks.backendweb.ktree.service.TreeService;
import com.baidu.mpks.backendweb.ktree.vo.TreeNodeVO;
import com.baidu.mpks.common.AbstractController;
import com.baidu.mpks.common.dto.CommonResponse;
import com.baidu.mpks.common.dto.ListDto;
import com.baidu.mpks.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/tree")
@Slf4j
public class TreeController extends AbstractController {
    @Autowired
    TreeService treeService;

    @Autowired
    TreeNodeDao treeNodeDao;

    @Autowired
    private SecurityUtil securityUtil;

    // 获取知识树
    @GetMapping(value = "/getNodes")
    public CommonResponse<ListDto<TreeNode>> getNodes() {
        return createSuccessList(treeService.getTreeNodes(), null);
    }

    // 获取知识树
    @GetMapping(value = "/getNodesByPermission")
    public  CommonResponse<ListDto<TreeNode>> getNodesByPermission(
            @RequestParam(value = "nodeId", required = false, defaultValue = "1") Integer nodeId) {
        Set<Integer> nodeIdSet = treeService.getChildPermittedNode(nodeId);
        List<TreeNode> treeNodes = treeNodeDao.getNodeByIds(nodeIdSet);
        if (!nodeIdSet.contains(nodeId)) {
            TreeNode selfNode = treeNodeDao.getNodeById(nodeId);
            treeNodes.add(selfNode);
        }

        return createSuccessList(treeNodes);
    }

    // 根据角色信息获取列表
    @GetMapping(value = "/getNodesByRoleId")
    public CommonResponse<ListDto<TreeNode>> getNodesByRoleId(@RequestParam("roleId") Integer roleId) {
        return createSuccessList(treeService.getNodesByRoleId(roleId), null);

    }

    // 获取单个节点信息
    @GetMapping(value = "/getNodeById")
    public CommonResponse<TreeNodeDto> getNodesById(@RequestParam("nodeId") Integer nodeId) {
        TreeNodeDto result = treeService.getNodeInfoById(nodeId);
        return createSuccessResponse(result);
    }

    // 目录树更新节点
    @PostMapping(value = "/update")
    public CommonResponse<TreeNode> updateTreeNode(@RequestBody @Valid TreeNodeVO treeNodeVO)
            throws IllegalAccessException, InvocationTargetException {
        TreeNode treeNode = new TreeNode(securityUtil.getUserIdOrDefault(""));
        BeanUtils.copyProperties(treeNode, treeNodeVO);
        treeService.updateTree(treeNode);

        return createSuccessResponse(treeNode);
    }

    // 目录树增加节点
    @PostMapping(value = "/add")
    public CommonResponse<TreeNode> addTreeNode(@RequestBody @Valid TreeNodeVO treeNodeVO) {
        TreeNode treeNode = new TreeNode(securityUtil.getUserIdOrDefault(""));
        try {
            BeanUtils.copyProperties(treeNode, treeNodeVO);
            if (treeNodeVO.getSort() == null) {
                treeNode.setSort(0);
            }
            treeService.addTreeNode(treeNode);
        } catch (IllegalAccessException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            log.error("" + e);
        }
        return createSuccessResponse(treeNode);
    }

    // 目录树删除节点
    @PostMapping(value = "/pos/delete")
    public CommonResponse deleteTreeNode(@RequestBody TreeNode treeNode) {
        treeService.delete(treeNode.getId());
        return createEmptySuccessResponse();
    }

    // 获取节点的子节点数
    @GetMapping(value = "getChild")
    public CommonResponse<KTreeResponse> getChildren(@RequestParam("nodeId") Integer nodeId) {

        Set<Integer> nodeIdsInt = treeService.getChildrensNotIncludeSelf(nodeId);
        KTreeResponse resp =  new KTreeResponse();
        resp.setCount(nodeIdsInt.size());
        return createSuccessResponse(resp);
    }

    // 根据节点获取该节点以及子节点的所有资源
    @GetMapping(value = "getSourceList")
    public CommonResponse<KTreeResponse> getSourceList(@RequestParam("nodeId") Integer nodeId) {
        Map<String, Object> data = new HashMap<>();
        KTreeResponse resp =  new KTreeResponse();
        resp.setItems(treeService.getSourceListByNodeId(nodeId));
        return createSuccessResponse(resp);
    }

    // 获取所有父类
    @GetMapping(value = "getAllParent")
    public CommonResponse<List<TreeNode>> getParentPath(@RequestParam("nodeId") Integer nodeId) {
        return createSuccessResponse(treeService.getAllParent(nodeId));
    }
}
